//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.meritdata.cloud.resultmodel;

import java.io.PrintWriter;
import java.io.Serializable;
import java.io.StringWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class ResultBody<T> implements Serializable {
    private static final long serialVersionUID = -3948389268046368059L;
    private Date timestamp = new Date();
    private Integer status;
    private String error;
    private String message;
    private String stackmessage;
    private String url;
    private T data;
    private Map<String, String> extraInfo = new HashMap();
    private boolean success;

    public ResultBody() {
    }

    public Date getTimestamp() {
        return this.timestamp;
    }

    public static long getSerialVersionUID() {
        return -3948389268046368059L;
    }

    public String getUrl() {
        HttpServletRequest request = ((ServletRequestAttributes)((ServletRequestAttributes)RequestContextHolder.currentRequestAttributes())).getRequest();
        return request.getRequestURI().toString();
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Integer getStatus() {
        return this.status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public String getMessage() {
        return this.message != null ? this.message : ResultStatus.getMessage(this.status);
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getStackmessage() {
        return this.stackmessage;
    }

    public void setStackmessage(String stackmessage) {
        this.stackmessage = stackmessage;
    }

    public void addExtraInfo(String key, String value) {
        this.extraInfo.put(key, value);
    }

    public Map<String, String> getExtraInfo() {
        return this.extraInfo;
    }

    public static ResultBody success() {
        ResultBody result = new ResultBody();
        result.setResultStatus(ResultStatus.SUCCESS);
        result.setSuccess(true);
        return result;
    }

    public static ResultBody success(Object data) {
        ResultBody result = new ResultBody();
        result.setResultStatus(ResultStatus.SUCCESS);
        result.setData(data);
        result.setSuccess(true);
        return result;
    }

    public static ResultBody failure(ResultStatus resultCode) {
        ResultBody result = new ResultBody();
        result.setResultStatus(resultCode);
        result.setSuccess(false);
        return result;
    }

    public static ResultBody failure(ResultStatus resultCode, Throwable e) {
        ResultBody result = new ResultBody();
        result.setResultStatus(resultCode);
        result.setStackmessage(getStackTrace(e));
        result.setSuccess(false);
        return result;
    }

    public static ResultBody failure(ResultStatus resultStatus, Object data) {
        ResultBody result = new ResultBody();
        result.setResultStatus(resultStatus);
        result.setData(data);
        result.setSuccess(false);
        return result;
    }

    public static ResultBody failure(Integer status, String message, Object data, Throwable e) {
        ResultBody result = new ResultBody();
        result.setStatus(status);
        result.setMessage(message);
        result.setData(data);
        result.setStackmessage(getStackTrace(e));
        result.setSuccess(false);
        return result;
    }

    public void setResultStatus(ResultStatus resultStatus) {
        this.status = resultStatus.status();
        this.message = resultStatus.message();
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public static String getStackTrace(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);

        String var3;
        try {
            if (t == null) {
                var3 = "";
                return var3;
            }

            t.printStackTrace(pw);
            var3 = sw.toString();
        } finally {
            pw.close();
        }

        return var3;
    }
}
